using System;
using System.Collections;
using System.Configuration;
using System.Data;

namespace BeeldBank.Business
{
    public class Luw
    {
        public Luw(string userName)
        {
            this.UserName = userName;
        }

        #region UserName
        private string _userName;
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
            }
        }
        #endregion
    
        public delegate void PrepareForSaveHandler();
        public event PrepareForSaveHandler OnPrepareForSave;
        public delegate void PreSaveHandler();
        public event PreSaveHandler OnPreSave;
        public delegate void SaveHandler();
        public event SaveHandler OnSave;
        public delegate void ClearHandler();
        public event ClearHandler OnClear;

        #region Save
        public void Save()
        {
            System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection();
            try
            {
                connection.ConnectionString = ConfigurationManager.AppSettings["connectstringSql"];
                
                connection.Open();
                _transaction = connection.BeginTransaction();
        
                // 1. PrepareForSave: laad vriendjes en vriendinnetjes
                // 2. PreSave: change je eigen properties
                // 3. Save: Write changes to database.
                if (OnPrepareForSave != null)
                {
                    OnPrepareForSave();
                }
                if (OnPreSave != null)
                {
                    OnPreSave();
                }
                if (OnSave != null)
                {
                    _canLoad = false;
                    OnSave();
                }
                if (OnClear != null)
                {
                    OnClear();
                }
                _transaction.Commit();
            }
            catch 
            {
                throw;
            }
            finally
            {
                connection.Close();
            }		
        }
        #endregion

        #region Transaction
        private System.Data.IDbTransaction _transaction;
        public System.Data.IDbTransaction Transaction
        {
            get
            {
                return _transaction;
            }
        }
        #endregion

        #region CanLoad
        private bool _canLoad = true;
        public bool CanLoad
        {
            get
            {
                return _canLoad;
            }
        }
        #endregion

        #region OpenObjects

        #region OpenCollectie
        public static Collectie OpenCollectie(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Collectie.Open(Id);
            Collectie Collectie = luw.NewCollectie(table.Rows[0]);
            return Collectie;
        }

        public Collectie OpenCollectie(int Id)
        {
            DataTable table = BeeldBank.Data.Collectie.Open(Id);
            Collectie Collectie = NewCollectie(table.Rows[0]);
            return Collectie;
        }
        #endregion

        #region OpenContactInfo
        public static ContactInfo OpenContactInfo(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.ContactInfo.Open(Id);
            ContactInfo ContactInfo = luw.NewContactInfo(table.Rows[0]);
            return ContactInfo;
        }

        public ContactInfo OpenContactInfo(int Id)
        {
            DataTable table = BeeldBank.Data.ContactInfo.Open(Id);
            ContactInfo ContactInfo = NewContactInfo(table.Rows[0]);
            return ContactInfo;
        }
        #endregion

        #region OpenDigital
        public static Digital OpenDigital(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Digital.Open(Id);
            Digital Digital = luw.NewDigital(table.Rows[0]);
            return Digital;
        }

        public Digital OpenDigital(int Id)
        {
            DataTable table = BeeldBank.Data.Digital.Open(Id);
            Digital Digital = NewDigital(table.Rows[0]);
            return Digital;
        }
        #endregion

        #region OpenMediaObject
        public static MediaObject OpenMediaObject(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.MediaObject.Open(Id);
            MediaObject MediaObject = luw.NewMediaObject(table.Rows[0]);
            return MediaObject;
        }

        public MediaObject OpenMediaObject(int Id)
        {
            DataTable table = BeeldBank.Data.MediaObject.Open(Id);
            MediaObject MediaObject = NewMediaObject(table.Rows[0]);
            return MediaObject;
        }
        #endregion

        #region OpenOrder
        public static Order OpenOrder(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Order.Open(Id);
            Order Order = luw.NewOrder(table.Rows[0]);
            return Order;
        }

        public Order OpenOrder(int Id)
        {
            DataTable table = BeeldBank.Data.Order.Open(Id);
            Order Order = NewOrder(table.Rows[0]);
            return Order;
        }
        #endregion

        #region OpenOrderRegel
        public static OrderRegel OpenOrderRegel(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.OrderRegel.Open(Id);
            OrderRegel OrderRegel = luw.NewOrderRegel(table.Rows[0]);
            return OrderRegel;
        }

        public OrderRegel OpenOrderRegel(int Id)
        {
            DataTable table = BeeldBank.Data.OrderRegel.Open(Id);
            OrderRegel OrderRegel = NewOrderRegel(table.Rows[0]);
            return OrderRegel;
        }
        #endregion

        #region OpenPrijsInfo
        public static PrijsInfo OpenPrijsInfo(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.PrijsInfo.Open(Id);
            PrijsInfo PrijsInfo = luw.NewPrijsInfo(table.Rows[0]);
            return PrijsInfo;
        }

        public PrijsInfo OpenPrijsInfo(int Id)
        {
            DataTable table = BeeldBank.Data.PrijsInfo.Open(Id);
            PrijsInfo PrijsInfo = NewPrijsInfo(table.Rows[0]);
            return PrijsInfo;
        }
        #endregion

        #region OpenSetting
        public static Setting OpenSetting(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Setting.Open(Id);
            Setting Setting = luw.NewSetting(table.Rows[0]);
            return Setting;
        }

        public Setting OpenSetting(int Id)
        {
            DataTable table = BeeldBank.Data.Setting.Open(Id);
            Setting Setting = NewSetting(table.Rows[0]);
            return Setting;
        }
        #endregion

        #region OpenStatus
        public static Status OpenStatus(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Status.Open(Id);
            Status Status = luw.NewStatus(table.Rows[0]);
            return Status;
        }

        public Status OpenStatus(int Id)
        {
            DataTable table = BeeldBank.Data.Status.Open(Id);
            Status Status = NewStatus(table.Rows[0]);
            return Status;
        }
        #endregion

        #region OpenType
        public static Type OpenType(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Type.Open(Id);
            Type Type = luw.NewType(table.Rows[0]);
            return Type;
        }

        public Type OpenType(int Id)
        {
            DataTable table = BeeldBank.Data.Type.Open(Id);
            Type Type = NewType(table.Rows[0]);
            return Type;
        }
        #endregion

        #region OpenUser
        public static User OpenUser(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.User.Open(Id);
            User User = luw.NewUser(table.Rows[0]);
            return User;
        }

        public User OpenUser(int Id)
        {
            DataTable table = BeeldBank.Data.User.Open(Id);
            User User = NewUser(table.Rows[0]);
            return User;
        }
        #endregion

        #region OpenWord
        public static Word OpenWord(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Word.Open(Id);
            Word Word = luw.NewWord(table.Rows[0]);
            return Word;
        }

        public Word OpenWord(int Id)
        {
            DataTable table = BeeldBank.Data.Word.Open(Id);
            Word Word = NewWord(table.Rows[0]);
            return Word;
        }
        #endregion

        #region OpenWordItem
        public static WordItem OpenWordItem(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.WordItem.Open(Id);
            WordItem WordItem = luw.NewWordItem(table.Rows[0]);
            return WordItem;
        }

        public WordItem OpenWordItem(int Id)
        {
            DataTable table = BeeldBank.Data.WordItem.Open(Id);
            WordItem WordItem = NewWordItem(table.Rows[0]);
            return WordItem;
        }
        #endregion

        #region OpenZoekActieLog
        public static ZoekActieLog OpenZoekActieLog(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.ZoekActieLog.Open(Id);
            ZoekActieLog ZoekActieLog = luw.NewZoekActieLog(table.Rows[0]);
            return ZoekActieLog;
        }

        public ZoekActieLog OpenZoekActieLog(int Id)
        {
            DataTable table = BeeldBank.Data.ZoekActieLog.Open(Id);
            ZoekActieLog ZoekActieLog = NewZoekActieLog(table.Rows[0]);
            return ZoekActieLog;
        }
        #endregion

        #endregion

        #region ShowObjects

        #region ShowCollectie
        public static Collectie ShowCollectie(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Collectie.Show(Id);
            Collectie Collectie = luw.NewCollectie(table.Rows[0]);
            return Collectie;
        }

        public Collectie ShowCollectie(int Id)
        {
            DataTable table = BeeldBank.Data.Collectie.Show(Id);
            Collectie Collectie = NewCollectie(table.Rows[0]);
            return Collectie;
        }
        #endregion

        #region ShowContactInfo
        public static ContactInfo ShowContactInfo(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.ContactInfo.Show(Id);
            ContactInfo ContactInfo = luw.NewContactInfo(table.Rows[0]);
            return ContactInfo;
        }

        public ContactInfo ShowContactInfo(int Id)
        {
            DataTable table = BeeldBank.Data.ContactInfo.Show(Id);
            ContactInfo ContactInfo = NewContactInfo(table.Rows[0]);
            return ContactInfo;
        }
        #endregion

        #region ShowDigital
        public static Digital ShowDigital(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Digital.Show(Id);
            Digital Digital = luw.NewDigital(table.Rows[0]);
            return Digital;
        }

        public Digital ShowDigital(int Id)
        {
            DataTable table = BeeldBank.Data.Digital.Show(Id);
            Digital Digital = NewDigital(table.Rows[0]);
            return Digital;
        }
        #endregion

        #region ShowMediaObject
        public static MediaObject ShowMediaObject(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.MediaObject.Show(Id);
            MediaObject MediaObject = luw.NewMediaObject(table.Rows[0]);
            return MediaObject;
        }

        public MediaObject ShowMediaObject(int Id)
        {
            DataTable table = BeeldBank.Data.MediaObject.Show(Id);
            MediaObject MediaObject = NewMediaObject(table.Rows[0]);
            return MediaObject;
        }
        #endregion

        #region ShowOrder
        public static Order ShowOrder(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Order.Show(Id);
            Order Order = luw.NewOrder(table.Rows[0]);
            return Order;
        }

        public Order ShowOrder(int Id)
        {
            DataTable table = BeeldBank.Data.Order.Show(Id);
            Order Order = NewOrder(table.Rows[0]);
            return Order;
        }
        #endregion

        #region ShowOrderRegel
        public static OrderRegel ShowOrderRegel(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.OrderRegel.Show(Id);
            OrderRegel OrderRegel = luw.NewOrderRegel(table.Rows[0]);
            return OrderRegel;
        }

        public OrderRegel ShowOrderRegel(int Id)
        {
            DataTable table = BeeldBank.Data.OrderRegel.Show(Id);
            OrderRegel OrderRegel = NewOrderRegel(table.Rows[0]);
            return OrderRegel;
        }
        #endregion

        #region ShowPrijsInfo
        public static PrijsInfo ShowPrijsInfo(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.PrijsInfo.Show(Id);
            PrijsInfo PrijsInfo = luw.NewPrijsInfo(table.Rows[0]);
            return PrijsInfo;
        }

        public PrijsInfo ShowPrijsInfo(int Id)
        {
            DataTable table = BeeldBank.Data.PrijsInfo.Show(Id);
            PrijsInfo PrijsInfo = NewPrijsInfo(table.Rows[0]);
            return PrijsInfo;
        }
        #endregion

        #region ShowSetting
        public static Setting ShowSetting(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Setting.Show(Id);
            Setting Setting = luw.NewSetting(table.Rows[0]);
            return Setting;
        }

        public Setting ShowSetting(int Id)
        {
            DataTable table = BeeldBank.Data.Setting.Show(Id);
            Setting Setting = NewSetting(table.Rows[0]);
            return Setting;
        }
        #endregion

        #region ShowStatus
        public static Status ShowStatus(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Status.Show(Id);
            Status Status = luw.NewStatus(table.Rows[0]);
            return Status;
        }

        public Status ShowStatus(int Id)
        {
            DataTable table = BeeldBank.Data.Status.Show(Id);
            Status Status = NewStatus(table.Rows[0]);
            return Status;
        }
        #endregion

        #region ShowType
        public static Type ShowType(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Type.Show(Id);
            Type Type = luw.NewType(table.Rows[0]);
            return Type;
        }

        public Type ShowType(int Id)
        {
            DataTable table = BeeldBank.Data.Type.Show(Id);
            Type Type = NewType(table.Rows[0]);
            return Type;
        }
        #endregion

        #region ShowUser
        public static User ShowUser(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.User.Show(Id);
            User User = luw.NewUser(table.Rows[0]);
            return User;
        }

        public User ShowUser(int Id)
        {
            DataTable table = BeeldBank.Data.User.Show(Id);
            User User = NewUser(table.Rows[0]);
            return User;
        }
        #endregion

        #region ShowWord
        public static Word ShowWord(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.Word.Show(Id);
            Word Word = luw.NewWord(table.Rows[0]);
            return Word;
        }

        public Word ShowWord(int Id)
        {
            DataTable table = BeeldBank.Data.Word.Show(Id);
            Word Word = NewWord(table.Rows[0]);
            return Word;
        }
        #endregion

        #region ShowWordItem
        public static WordItem ShowWordItem(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.WordItem.Show(Id);
            WordItem WordItem = luw.NewWordItem(table.Rows[0]);
            return WordItem;
        }

        public WordItem ShowWordItem(int Id)
        {
            DataTable table = BeeldBank.Data.WordItem.Show(Id);
            WordItem WordItem = NewWordItem(table.Rows[0]);
            return WordItem;
        }
        #endregion

        #region ShowZoekActieLog
        public static ZoekActieLog ShowZoekActieLog(int Id, string userName)
        {
            Luw luw = new Luw(userName);
            DataTable table = BeeldBank.Data.ZoekActieLog.Show(Id);
            ZoekActieLog ZoekActieLog = luw.NewZoekActieLog(table.Rows[0]);
            return ZoekActieLog;
        }

        public ZoekActieLog ShowZoekActieLog(int Id)
        {
            DataTable table = BeeldBank.Data.ZoekActieLog.Show(Id);
            ZoekActieLog ZoekActieLog = NewZoekActieLog(table.Rows[0]);
            return ZoekActieLog;
        }
        #endregion

        #endregion

        #region NewObjects

        #region NewCollectie
        public Collectie NewCollectie(DataRow row)
        {
            Collectie collectie = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                collectie = this.GetCollectie(Convert.ToInt32(row["Id"]));
            }
            if (collectie == null)
            {
                collectie = new Collectie(row);
                collectie.IsDirty = true;
                collectie.State = BusinessState.Unchanged;
                collectie.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(collectie.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(collectie.PreSave);
                this.OnSave += new SaveHandler(collectie.Save);
                this.OnClear += new ClearHandler(collectie.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    collectie.State = BusinessState.New;
                    this.AddCollectie(collectie);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    collectie.State = BusinessState.Deleted;
                }
            }
            return collectie;
        }
        #endregion

        #region NewContactInfo
        public ContactInfo NewContactInfo(DataRow row)
        {
            ContactInfo contactInfo = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                contactInfo = this.GetContactInfo(Convert.ToInt32(row["Id"]));
            }
            if (contactInfo == null)
            {
                contactInfo = new ContactInfo(row);
                contactInfo.IsDirty = true;
                contactInfo.State = BusinessState.Unchanged;
                contactInfo.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(contactInfo.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(contactInfo.PreSave);
                this.OnSave += new SaveHandler(contactInfo.Save);
                this.OnClear += new ClearHandler(contactInfo.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    contactInfo.State = BusinessState.New;
                    this.AddContactInfo(contactInfo);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    contactInfo.State = BusinessState.Deleted;
                }
            }
            return contactInfo;
        }
        #endregion

        #region NewDigital
        public Digital NewDigital(DataRow row)
        {
            Digital digital = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                digital = this.GetDigital(Convert.ToInt32(row["Id"]));
            }
            if (digital == null)
            {
                digital = new Digital(row);
                digital.IsDirty = true;
                digital.State = BusinessState.Unchanged;
                digital.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(digital.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(digital.PreSave);
                this.OnSave += new SaveHandler(digital.Save);
                this.OnClear += new ClearHandler(digital.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    digital.State = BusinessState.New;
                    this.AddDigital(digital);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    digital.State = BusinessState.Deleted;
                }
            }
            return digital;
        }
        #endregion

        #region NewMediaObject
        public MediaObject NewMediaObject(DataRow row)
        {
            MediaObject mediaObject = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                mediaObject = this.GetMediaObject(Convert.ToInt32(row["Id"]));
            }
            if (mediaObject == null)
            {
                mediaObject = new MediaObject(row);
                mediaObject.IsDirty = true;
                mediaObject.State = BusinessState.Unchanged;
                mediaObject.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(mediaObject.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(mediaObject.PreSave);
                this.OnSave += new SaveHandler(mediaObject.Save);
                this.OnClear += new ClearHandler(mediaObject.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    mediaObject.State = BusinessState.New;
                    this.AddMediaObject(mediaObject);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    mediaObject.State = BusinessState.Deleted;
                }
            }
            return mediaObject;
        }
        #endregion

        #region NewOrder
        public Order NewOrder(DataRow row)
        {
            Order order = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                order = this.GetOrder(Convert.ToInt32(row["Id"]));
            }
            if (order == null)
            {
                order = new Order(row);
                order.IsDirty = true;
                order.State = BusinessState.Unchanged;
                order.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(order.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(order.PreSave);
                this.OnSave += new SaveHandler(order.Save);
                this.OnClear += new ClearHandler(order.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    order.State = BusinessState.New;
                    this.AddOrder(order);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    order.State = BusinessState.Deleted;
                }
            }
            return order;
        }
        #endregion

        #region NewOrderRegel
        public OrderRegel NewOrderRegel(DataRow row)
        {
            OrderRegel orderRegel = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                orderRegel = this.GetOrderRegel(Convert.ToInt32(row["Id"]));
            }
            if (orderRegel == null)
            {
                orderRegel = new OrderRegel(row);
                orderRegel.IsDirty = true;
                orderRegel.State = BusinessState.Unchanged;
                orderRegel.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(orderRegel.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(orderRegel.PreSave);
                this.OnSave += new SaveHandler(orderRegel.Save);
                this.OnClear += new ClearHandler(orderRegel.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    orderRegel.State = BusinessState.New;
                    this.AddOrderRegel(orderRegel);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    orderRegel.State = BusinessState.Deleted;
                }
            }
            return orderRegel;
        }
        #endregion

        #region NewPrijsInfo
        public PrijsInfo NewPrijsInfo(DataRow row)
        {
            PrijsInfo prijsInfo = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                prijsInfo = this.GetPrijsInfo(Convert.ToInt32(row["Id"]));
            }
            if (prijsInfo == null)
            {
                prijsInfo = new PrijsInfo(row);
                prijsInfo.IsDirty = true;
                prijsInfo.State = BusinessState.Unchanged;
                prijsInfo.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(prijsInfo.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(prijsInfo.PreSave);
                this.OnSave += new SaveHandler(prijsInfo.Save);
                this.OnClear += new ClearHandler(prijsInfo.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    prijsInfo.State = BusinessState.New;
                    this.AddPrijsInfo(prijsInfo);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    prijsInfo.State = BusinessState.Deleted;
                }
            }
            return prijsInfo;
        }
        #endregion

        #region NewSetting
        public Setting NewSetting(DataRow row)
        {
            Setting setting = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                setting = this.GetSetting(Convert.ToInt32(row["Id"]));
            }
            if (setting == null)
            {
                setting = new Setting(row);
                setting.IsDirty = true;
                setting.State = BusinessState.Unchanged;
                setting.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(setting.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(setting.PreSave);
                this.OnSave += new SaveHandler(setting.Save);
                this.OnClear += new ClearHandler(setting.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    setting.State = BusinessState.New;
                    this.AddSetting(setting);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    setting.State = BusinessState.Deleted;
                }
            }
            return setting;
        }
        #endregion

        #region NewStatus
        public Status NewStatus(DataRow row)
        {
            Status status = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                status = this.GetStatus(Convert.ToInt32(row["Id"]));
            }
            if (status == null)
            {
                status = new Status(row);
                status.IsDirty = true;
                status.State = BusinessState.Unchanged;
                status.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(status.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(status.PreSave);
                this.OnSave += new SaveHandler(status.Save);
                this.OnClear += new ClearHandler(status.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    status.State = BusinessState.New;
                    this.AddStatus(status);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    status.State = BusinessState.Deleted;
                }
            }
            return status;
        }
        #endregion

        #region NewType
        public Type NewType(DataRow row)
        {
            Type type = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                type = this.GetType(Convert.ToInt32(row["Id"]));
            }
            if (type == null)
            {
                type = new Type(row);
                type.IsDirty = true;
                type.State = BusinessState.Unchanged;
                type.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(type.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(type.PreSave);
                this.OnSave += new SaveHandler(type.Save);
                this.OnClear += new ClearHandler(type.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    type.State = BusinessState.New;
                    this.AddType(type);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    type.State = BusinessState.Deleted;
                }
            }
            return type;
        }
        #endregion

        #region NewUser
        public User NewUser(DataRow row)
        {
            User user = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                user = this.GetUser(Convert.ToInt32(row["Id"]));
            }
            if (user == null)
            {
                user = new User(row);
                user.IsDirty = true;
                user.State = BusinessState.Unchanged;
                user.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(user.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(user.PreSave);
                this.OnSave += new SaveHandler(user.Save);
                this.OnClear += new ClearHandler(user.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    user.State = BusinessState.New;
                    this.AddUser(user);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    user.State = BusinessState.Deleted;
                }
            }
            return user;
        }
        #endregion

        #region NewWord
        public Word NewWord(DataRow row)
        {
            Word word = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                word = this.GetWord(Convert.ToInt32(row["Id"]));
            }
            if (word == null)
            {
                word = new Word(row);
                word.IsDirty = true;
                word.State = BusinessState.Unchanged;
                word.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(word.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(word.PreSave);
                this.OnSave += new SaveHandler(word.Save);
                this.OnClear += new ClearHandler(word.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    word.State = BusinessState.New;
                    this.AddWord(word);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    word.State = BusinessState.Deleted;
                }
            }
            return word;
        }
        #endregion

        #region NewWordItem
        public WordItem NewWordItem(DataRow row)
        {
            WordItem wordItem = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                wordItem = this.GetWordItem(Convert.ToInt32(row["Id"]));
            }
            if (wordItem == null)
            {
                wordItem = new WordItem(row);
                wordItem.IsDirty = true;
                wordItem.State = BusinessState.Unchanged;
                wordItem.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(wordItem.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(wordItem.PreSave);
                this.OnSave += new SaveHandler(wordItem.Save);
                this.OnClear += new ClearHandler(wordItem.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    wordItem.State = BusinessState.New;
                    this.AddWordItem(wordItem);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    wordItem.State = BusinessState.Deleted;
                }
            }
            return wordItem;
        }
        #endregion

        #region NewZoekActieLog
        public ZoekActieLog NewZoekActieLog(DataRow row)
        {
            ZoekActieLog zoekActieLog = null;
            if (row == null)
            {
                return null;
            }
            if (row["Id"] != Convert.DBNull)
            {
                zoekActieLog = this.GetZoekActieLog(Convert.ToInt32(row["Id"]));
            }
            if (zoekActieLog == null)
            {
                zoekActieLog = new ZoekActieLog(row);
                zoekActieLog.IsDirty = true;
                zoekActieLog.State = BusinessState.Unchanged;
                zoekActieLog.Luw = this;
                this.OnPrepareForSave += new PrepareForSaveHandler(zoekActieLog.PrepareForSaveBase);
                this.OnPreSave += new PreSaveHandler(zoekActieLog.PreSave);
                this.OnSave += new SaveHandler(zoekActieLog.Save);
                this.OnClear += new ClearHandler(zoekActieLog.Clear);
                if (row.RowState == DataRowState.Added)
                {
                    zoekActieLog.State = BusinessState.New;
                    this.AddZoekActieLog(zoekActieLog);
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    zoekActieLog.State = BusinessState.Deleted;
                }
            }
            return zoekActieLog;
        }
        #endregion

        #endregion

        #region ClearIdentityMaps
        public void ClearIdentityMaps()
        {
            identityMapCollectie = null;
            identityMapContactInfo = null;
            identityMapDigital = null;
            identityMapMediaObject = null;
            identityMapOrder = null;
            identityMapOrderRegel = null;
            identityMapPrijsInfo = null;
            identityMapSetting = null;
            identityMapStatus = null;
            identityMapType = null;
            identityMapUser = null;
            identityMapWord = null;
            identityMapWordItem = null;
            identityMapZoekActieLog = null;
        }
        #endregion

        #region IdentityMapCollectie
        private Hashtable identityMapCollectie;
        public void AddCollectie(Collectie collectie)
        {
            if (identityMapCollectie == null)
            {
                identityMapCollectie = new Hashtable();
            }
            identityMapCollectie.Add(collectie.Id, collectie);
        }

        public Collectie GetCollectie(int Id)
        {
            if (identityMapCollectie == null)
            {
                return null;
            }
            return (Collectie)identityMapCollectie[Id];
        }
        #endregion

        #region IdentityMapContactInfo
        private Hashtable identityMapContactInfo;
        public void AddContactInfo(ContactInfo contactInfo)
        {
            if (identityMapContactInfo == null)
            {
                identityMapContactInfo = new Hashtable();
            }
            identityMapContactInfo.Add(contactInfo.Id, contactInfo);
        }

        public ContactInfo GetContactInfo(int Id)
        {
            if (identityMapContactInfo == null)
            {
                return null;
            }
            return (ContactInfo)identityMapContactInfo[Id];
        }
        #endregion

        #region IdentityMapDigital
        private Hashtable identityMapDigital;
        public void AddDigital(Digital digital)
        {
            if (identityMapDigital == null)
            {
                identityMapDigital = new Hashtable();
            }
            identityMapDigital.Add(digital.Id, digital);
        }

        public Digital GetDigital(int Id)
        {
            if (identityMapDigital == null)
            {
                return null;
            }
            return (Digital)identityMapDigital[Id];
        }
        #endregion

        #region IdentityMapMediaObject
        private Hashtable identityMapMediaObject;
        public void AddMediaObject(MediaObject mediaObject)
        {
            if (identityMapMediaObject == null)
            {
                identityMapMediaObject = new Hashtable();
            }
            identityMapMediaObject.Add(mediaObject.Id, mediaObject);
        }

        public MediaObject GetMediaObject(int Id)
        {
            if (identityMapMediaObject == null)
            {
                return null;
            }
            return (MediaObject)identityMapMediaObject[Id];
        }
        #endregion

        #region IdentityMapOrder
        private Hashtable identityMapOrder;
        public void AddOrder(Order order)
        {
            if (identityMapOrder == null)
            {
                identityMapOrder = new Hashtable();
            }
            identityMapOrder.Add(order.Id, order);
        }

        public Order GetOrder(int Id)
        {
            if (identityMapOrder == null)
            {
                return null;
            }
            return (Order)identityMapOrder[Id];
        }
        #endregion

        #region IdentityMapOrderRegel
        private Hashtable identityMapOrderRegel;
        public void AddOrderRegel(OrderRegel orderRegel)
        {
            if (identityMapOrderRegel == null)
            {
                identityMapOrderRegel = new Hashtable();
            }
            identityMapOrderRegel.Add(orderRegel.Id, orderRegel);
        }

        public OrderRegel GetOrderRegel(int Id)
        {
            if (identityMapOrderRegel == null)
            {
                return null;
            }
            return (OrderRegel)identityMapOrderRegel[Id];
        }
        #endregion

        #region IdentityMapPrijsInfo
        private Hashtable identityMapPrijsInfo;
        public void AddPrijsInfo(PrijsInfo prijsInfo)
        {
            if (identityMapPrijsInfo == null)
            {
                identityMapPrijsInfo = new Hashtable();
            }
            identityMapPrijsInfo.Add(prijsInfo.Id, prijsInfo);
        }

        public PrijsInfo GetPrijsInfo(int Id)
        {
            if (identityMapPrijsInfo == null)
            {
                return null;
            }
            return (PrijsInfo)identityMapPrijsInfo[Id];
        }
        #endregion

        #region IdentityMapSetting
        private Hashtable identityMapSetting;
        public void AddSetting(Setting setting)
        {
            if (identityMapSetting == null)
            {
                identityMapSetting = new Hashtable();
            }
            identityMapSetting.Add(setting.Id, setting);
        }

        public Setting GetSetting(int Id)
        {
            if (identityMapSetting == null)
            {
                return null;
            }
            return (Setting)identityMapSetting[Id];
        }
        #endregion

        #region IdentityMapStatus
        private Hashtable identityMapStatus;
        public void AddStatus(Status status)
        {
            if (identityMapStatus == null)
            {
                identityMapStatus = new Hashtable();
            }
            identityMapStatus.Add(status.Id, status);
        }

        public Status GetStatus(int Id)
        {
            if (identityMapStatus == null)
            {
                return null;
            }
            return (Status)identityMapStatus[Id];
        }
        #endregion

        #region IdentityMapType
        private Hashtable identityMapType;
        public void AddType(Type type)
        {
            if (identityMapType == null)
            {
                identityMapType = new Hashtable();
            }
            identityMapType.Add(type.Id, type);
        }

        public Type GetType(int Id)
        {
            if (identityMapType == null)
            {
                return null;
            }
            return (Type)identityMapType[Id];
        }
        #endregion

        #region IdentityMapUser
        private Hashtable identityMapUser;
        public void AddUser(User user)
        {
            if (identityMapUser == null)
            {
                identityMapUser = new Hashtable();
            }
            identityMapUser.Add(user.Id, user);
        }

        public User GetUser(int Id)
        {
            if (identityMapUser == null)
            {
                return null;
            }
            return (User)identityMapUser[Id];
        }
        #endregion

        #region IdentityMapWord
        private Hashtable identityMapWord;
        public void AddWord(Word word)
        {
            if (identityMapWord == null)
            {
                identityMapWord = new Hashtable();
            }
            identityMapWord.Add(word.Id, word);
        }

        public Word GetWord(int Id)
        {
            if (identityMapWord == null)
            {
                return null;
            }
            return (Word)identityMapWord[Id];
        }
        #endregion

        #region IdentityMapWordItem
        private Hashtable identityMapWordItem;
        public void AddWordItem(WordItem wordItem)
        {
            if (identityMapWordItem == null)
            {
                identityMapWordItem = new Hashtable();
            }
            identityMapWordItem.Add(wordItem.Id, wordItem);
        }

        public WordItem GetWordItem(int Id)
        {
            if (identityMapWordItem == null)
            {
                return null;
            }
            return (WordItem)identityMapWordItem[Id];
        }
        #endregion

        #region IdentityMapZoekActieLog
        private Hashtable identityMapZoekActieLog;
        public void AddZoekActieLog(ZoekActieLog zoekActieLog)
        {
            if (identityMapZoekActieLog == null)
            {
                identityMapZoekActieLog = new Hashtable();
            }
            identityMapZoekActieLog.Add(zoekActieLog.Id, zoekActieLog);
        }

        public ZoekActieLog GetZoekActieLog(int Id)
        {
            if (identityMapZoekActieLog == null)
            {
                return null;
            }
            return (ZoekActieLog)identityMapZoekActieLog[Id];
        }
        #endregion

    }
}
